import {observable,action, toJS} from 'mobx';
import {Tools,Bubble} from 'weaver-mobile';
import {getTheme} from '../util/pulic-func'
import * as API from '../api/invoiceCloud';
import {getEquipment,getLang} from '../util/common';

const getLabel = Tools.getLabel;

export default class BaseStore {
    /********************************************************/
    @observable invoiceCloudInfo = {};
    @observable theme = {};
    @observable smallE = false;
    equipment = {};
    InvoiceCloudUrlArr = [
        '/invoiceWeb/taro/dist/h5/#/pages/service/wallet', //我的票夹  *tab页 
        '/invoiceWeb/taro/dist/h5/#/pages/service/setting',//我的票夹  *发票设置
        '/invoiceWeb/taro/dist/h5/#/pages/service/preview',//我的票夹  *预览
        '/invoiceWeb/taro/dist/h5/#/pages/service/entry/ec',//发票录入
        '/invoiceWeb/taro/dist/h5/#/pages/service/expense/ec',//记一笔  *添加发票
        '/invoiceWeb/taro/dist/h5/#/pages/invoice/preview/index',//记一笔 *添加发票 点击发票
        '/invoiceWeb/taro/dist/h5/#/pages/service/hdr', //应用 *开票助手
    ];

    //获取token
    getInvoiceCloudToken = (value={},callBack) =>{
        API.getInvoiceCloudToken(value).then( res =>{
            let {invoiceCloud = false,errorInfo} = res;

                if(invoiceCloud){//发票云
                    if(errorInfo){
                        Bubble.error(errorInfo);
                    }else{
                        this.invoiceCloudInfo = {
                            token: res.token,
                            invoiceCloud: invoiceCloud,
                            jsUrl:res.jsUrl,
                        }
                    }
                }else{
                    this.invoiceCloudInfo = {
                        invoiceCloud: invoiceCloud
                    }
                }

                callBack && callBack(); 
                

                // Bubble.error(errorInfo);
            
           
        }) 
           
    }

    //获取主题
    getTheme = (value={}) =>{
        getTheme((_obj)=>{
            if(_obj){
                if(_obj.errCode == '0'){
                    this.theme = {
                        primary: _obj.clientTheme,
                        // ..._obj,
                    };
                }else{
                    Bubble.error(_obj);
                }
            }
        });

    }

    getEquipmentFn = () =>{
        this.equipment = getEquipment();
    }

    //获取首页 底部 配置
    getHomeFooter = (value={}) =>{
        API.microReportBottom(value).then( res =>{
            const {status} = res;
            if(status == '1'){
                this.smallE = res.smallE;
            }
        })
    }

    //统一调用 发票云页面
    getInvoiceCloudUrl = (route,type,arg) => {
        if(!this.invoiceCloudInfo.token) return;
        let devNum = 0;  //0：PC    1：IOS   2：ANDROID   3：H5   4：企业微信APP   5：企业微信PC   6：微信公众号
        
        if(this.equipment.isDT === true ){
            devNum = 7;
        }else if(this.equipment.isWx === true ){
            devNum = 6;
        }else if(this.equipment.isWx === false){
            devNum = 4;
        }else if(this.equipment.isAndroid || this.equipment.isiOS){
            devNum = 3;
        }
		
		let lang = getLang();
        let url = `${this.invoiceCloudInfo.jsUrl}${this.InvoiceCloudUrlArr[type]}?token=${this.invoiceCloudInfo.token}&src=EC&dev=${devNum}&lang=${lang}`;

        if(arg){
            Object.keys(arg).forEach( (ele)=>{
                url += `&${ele}=${arg[ele]}`;            
            })
        }

        return url
        
    }


    

}